﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.membercenter.accountrun
{
    public partial class receaddress_list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["MemberID"] == null)
                {
                    ChangeHope.WebPage.Script.AlertAndGoBack("温馨提示：登录超时，请重新登录！");
                }
                if (ChangeHope.WebPage.PageRequest.GetFormString("Option") != string.Empty && ChangeHope.WebPage.PageRequest.GetFormString("id") != string.Empty)
                {
                    string types = Request.Form["Option"].Trim();
                    string StrID = ChangeHope.WebPage.PageRequest.GetFormString("id");
                    if (types == "del")
                    {
                        Del(StrID);
                    }
                    Response.End();
                    return;
                }
                this.lblList.Text = GetList(Session["MemberID"].ToString());
            }
        }

        protected string GetList(string uid)
        {
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            YXShop.BLL.Member.ReceAddress bll = new YXShop.BLL.Member.ReceAddress();
            ChangeHope.DataBase.DataByPage dataPage = bll.GetList(" 1=1 and uid=" + uid);
            //第一步先添加表头
            table.AddHeadCol("", "联系人");
            table.AddHeadCol("", "移动电话");
            table.AddHeadCol("", "座机");
            table.AddHeadCol("", "详细地址");
            table.AddHeadCol("", "邮箱");
            table.AddHeadCol("", "是否默认");
            table.AddHeadCol("", "操作");
            table.AddRow();
            //添加表的内容
            if (dataPage.DataReader != null)
            {
                while (dataPage.DataReader.Read())
                {
                    string url = YXShop.TemplateAction.Common.UrlStr("ReceAddress_edite", "addressId=" + dataPage.DataReader["id"].ToString(), "membercenter/accountrun/");
                    string address = ChangeHope.Common.StringHelper.SubStringAndAppend(dataPage.DataReader["address"].ToString(), 30, "...");

                    table.AddCol(dataPage.DataReader["username"].ToString());
                    table.AddCol(dataPage.DataReader["mobile"].ToString());
                    table.AddCol(dataPage.DataReader["phone"].ToString());
                    table.AddCol(address);
                    table.AddCol(dataPage.DataReader["email"].ToString());
                    table.AddCol(Int32.Parse(dataPage.DataReader["stat"].ToString()) == 1 ? "<font color='green'>是</font>" : "<font color='red'>否</font>");
                    table.AddCol(string.Format("<a href='{1}'>编辑</a> <a href='javascript:void(0)' onclick='Del({0})'>删除</a>", dataPage.DataReader["id"].ToString(), url));
                    table.AddRow();
                }
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            return view;
        }

        private void Del(string id)
        {
            YXShop.BLL.Member.ReceAddress bll = new YXShop.BLL.Member.ReceAddress();
            bll.Delete(id);
            Response.Write("ok");
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            string url = YXShop.TemplateAction.Common.UrlStr("ReceAddress_edite", "", "membercenter/accountrun/");
            Response.Redirect(url);
        }
    }
}
